﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.News
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication())
            {
                string ID = Request.QueryString["ID"] ?? String.Empty;
                if (ID == String.Empty)
                {
                    Response.Redirect("~/Admin/News/Index.aspx");
                }
                else
                {
                    if (!IsPostBack)
                    {
                        LoadData();
                    }
                }
            }

        }
        private void LoadData()
        {
            info.InnerHtml = string.Empty;
            if ((string)Session["Info"] != "")
            {
                info.InnerHtml = (string)Session["Info"];
                Session["Info"] = "";
            }
            int ID = int.Parse(Request.QueryString["ID"]);
            BAI_VIET baiViet = BaiVietModels.GetByID(ID);
            if (baiViet != null)
            {
                txtNewsName.Value = baiViet.TieuDe;
                txtSummary.Value = baiViet.NoiDungTomTat;
                txtContent.Value = baiViet.NoiDungChiTiet;
                lstNewsType.InnerHtml = LoaiBaiVietModels.SelectNewsType(baiViet.IDLoaiBaiViet);
                imgBackground.ImageUrl = ResolveClientUrl("~/images/blog/default.png");
                if(baiViet.Hinh != null)
                {
                    imgBackground.ImageUrl = ResolveClientUrl("~/images/blog/" + baiViet.Hinh);
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtNewsName.Value != "")
            {
                int ID = int.Parse(Request.QueryString["ID"]);
                BAI_VIET baiViet = BaiVietModels.GetByID(ID);

                if (imgRoot.HasFile)
                {
                    string fileName = Utils.UploadImage(imgRoot.PostedFile, ResolveClientUrl("~/images/blog/"));
                    if (fileName == String.Empty)
                    {
                        Session["Info"] = "<div class='alert alert-danger'>Vui lòng chọn file đúng định dạng !!!</div>";                        
                        Response.Redirect("~/Admin/News/Update.aspx?ID=" + Request.QueryString["ID"]);
                    }
                    else
                    {
                        //xoa file cu
                        Utils.DeleteFile(ResolveClientUrl("~/images/blog/" + baiViet.Hinh));
                        baiViet.Hinh = fileName;
                    }
                }
                baiViet.IDLoaiBaiViet = int.Parse(Request.Form["newsType"]);                                
                baiViet.NoiDungChiTiet = txtContent.Value;
                baiViet.NoiDungTomTat = txtSummary.Value;
                baiViet.TieuDe = txtNewsName.Value;
                
                bool status = BaiVietModels.Update(baiViet);
                if (status)
                {
                    Session["Info"] = "<div class='alert alert-success'>Cập nhật tin tức thành công !!!</div>";
                    Response.Redirect("~/Admin/News/Index.aspx");
                }
                else
                {
                    info.InnerHtml = "<div class='alert alert-danger'>Đã xảy ra lỗi, vui lòng thử lại !!!</div>";
                }
            }
            else
            {
                info.InnerHtml = "<div class='alert alert-danger'>Vui lòng nhập đủ thông tin !!!</div>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/News/Index.aspx");
        }
    }
}